//
//  ZGLLoginDLView.m
//  Faker
//
//  Created by dllo on 15/11/10.
//  Copyright © 2015年 dllo. All rights reserved.
//
#import <AVOSCloud/AVOSCloud.h>
#import "ZGLLoginDLView.h"
@interface ZGLLoginDLView()<UITextFieldDelegate>
@property(nonatomic, retain)UITextField *name;
@property(nonatomic, retain)UITextField *pwd;
@end
@implementation ZGLLoginDLView
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor yellowColor];
        
        UILabel *nameLab = [[UILabel alloc]initWithFrame:CGRectMake(0, 40, 80, 40)];
        nameLab.text = @"用户名: ";
        nameLab.textAlignment = NSTextAlignmentRight;
        [self addSubview:nameLab];
        
        UILabel *pwdLab = [[UILabel alloc]initWithFrame:CGRectMake(0, 120, 80, 40)];
        pwdLab.text = @"密码: ";
        pwdLab.textAlignment = NSTextAlignmentRight;
        [self addSubview:pwdLab];
        
        _name = [[UITextField alloc]initWithFrame:CGRectMake(100*ScaleX, 40, 180, 40)];
        _name.placeholder = @"请输入用户名";
        _name.borderStyle = 3;
        _name.backgroundColor = [UIColor lightGrayColor];
        [_name becomeFirstResponder];
        _name.delegate = self;
        _name.tag = 3500;
        _name.keyboardType = UIKeyboardTypeDefault;
        [self addSubview:_name];
        
        _pwd = [[UITextField alloc]initWithFrame:CGRectMake(100*ScaleX, 120, 180, 40)];
        _pwd.placeholder = @"请输入密码";
        _pwd.borderStyle = 3;
        _pwd.backgroundColor = [UIColor lightGrayColor];
        _pwd.secureTextEntry = YES;//安全输入
        _pwd.keyboardType = UIKeyboardTypeASCIICapable;
        _pwd.delegate = self;
        _pwd.tag = 3501;
        [self addSubview:_pwd];
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
        button.frame = CGRectMake(120*ScaleX, 200, 40, 20);
        [button setTitle:@"登录" forState:UIControlStateNormal];
        button.selected = NO;
        [self addSubview:button];
        [button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    }
    return self;
}

- (void)click:(UIButton *)button{
    if (!button.selected) {
        if([_name.text isEqualToString:@""]||[_pwd.text isEqualToString:@""]) {
            [self arLog:@"不能为空"];
        }else{
            button.selected = YES;
            [AVUser logInWithUsernameInBackground:_name.text password:_pwd.text block:^(AVUser *user, NSError *error) {
                if (user) {
                    [self arLog:@"登陆成功"];
                    logName = [[AVUser currentUser] username];
                    //注册成功后也可以直接通过currentUser获取当前登陆用户
                    //[self arLog:[NSString stringWithFormat:@"当前用户 %@",[[AVUser currentUser] username]]];
                }else{
                    [self arLog:[NSString stringWithFormat:@"登陆出错,未找到用户 %@",[error description]]];
                }
                button.selected = NO;
            }];
        }
    }
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    switch (textField.tag) {
        case 3500:
            [_pwd becomeFirstResponder];
            break;            
        default:
            [textField resignFirstResponder];
            break;
    }
    return YES;
}

- (void)arLog:(NSString *)str{
    alartLog(str);
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
